/* eslint-disable jsx-a11y/anchor-has-content */
import { Radio, Space, Table, Tag } from 'antd';
import { useEffect } from 'react';
import Mock from 'mockjs';
import useCacheMakeList from '@/hooks/useCacheMakeList';
import request from '@/utils/req';

const topOptions = [
  {
    label: 'topLeft',
    value: 'topLeft',
  },
  {
    label: 'topCenter',
    value: 'topCenter',
  },
  {
    label: 'topRight',
    value: 'topRight',
  },
  {
    label: 'none',
    value: 'none',
  },
];
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    key: 'name',
    render: text => <a>{text}</a>,
  },
  {
    title: 'Age',
    dataIndex: 'age',
    key: 'age',
  },
  {
    title: 'Address',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title: 'Tags',
    key: 'tags',
    dataIndex: 'tags',
    render: tags => (
      <span>
        {tags.map(tag => {
          let color = tag.length > 5 ? 'geekblue' : 'green';
          if (tag === 'loser') {
            color = 'volcano';
          }
          return (
            <Tag color={color} key={tag}>
              {tag.toUpperCase()}
            </Tag>
          );
        })}
      </span>
    ),
  },
  {
    title: 'Action',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <a>Invite {record.name}</a>
        <a>Delete</a>
      </Space>
    ),
  },
];

const renderMockData = () =>
  Mock.mock({
    success: true,
    data: {
      'records|10': [
        {
          name: '@name',
          age: '@integer(10,100)',
          address: '@county(true)',
          'tags|2': ['cool', 'teacher', 'loser'],
          key: '@id',
        },
      ],
      total: 400,
    },
  });
const App = () => {
  const { list, getList, extraParams, setExtraParams, paginationConfig } = useCacheMakeList({
    api: {
      getList: params => {
        return request('/getMockList', {
          params,
        });
      },
    },
    params: {
      top: 'topLeft',
    },
  });
  const { top } = extraParams || {};

  useEffect(() => {
    getList();
  }, []);

  return (
    <div>
      <div>
        <Radio.Group
          style={{
            marginBottom: 10,
          }}
          options={topOptions}
          value={top}
          onChange={e => {
            setExtraParams(prev => ({ ...prev, top: e.target.value }));
            getList();
          }}
        />
      </div>
      <Table
        columns={columns}
        pagination={{
          position: [top],
          ...paginationConfig,
        }}
        dataSource={list}
      />
    </div>
  );
};
export default App;
